/*
 * @Author: gmh
 * @Date: 2024-01-09 15:08:10
 * @Description: 会员信息持久化
 */
import type { LoginResult } from '@/types/member'
import { defineStore } from 'pinia'
import { ref } from 'vue'

// 定义 Store
export const useMemberStore = defineStore(
  'member',
  () => {
    // 会员信息
    const profile = ref<LoginResult>()

    // 登录时持久化会员信息
    const setProfile = (val: LoginResult) => {
      profile.value = val
    }

    // 退出时清理会员信息
    const clearProfile = () => {
      profile.value = undefined
    }

    // return
    return {
      profile,
      setProfile,
      clearProfile,
    }
  },

  // TODO: 持久化
  {
    // 网页端配置
    // persist: true,

    //小程序配置
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          return uni.setStorageSync(key, value)
        },
      },
    },
  },
)
